package com.software.chemistry.controller;

import com.software.chemistry.domain.Result;
import com.software.chemistry.entity.Message;
import com.software.chemistry.entity.MsgList;
import com.software.chemistry.service.MessageService;
import com.software.chemistry.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

//沟通前后端
@RestController
@RequestMapping("/message")
public class MessageController {
    @Autowired
    private MessageService messageService ;

    //查询发送消息的人的id
    @GetMapping("/findMessagesByReceiverIdAndType/{receiverId}/{Type}")
    public Result<MsgList> findMessagesByReceiverIdAndType(@PathVariable String receiverId, @PathVariable long Type){
        System.out.println(1);
        List<MsgList> messages = messageService.findMessagesByReceiverIdAndType(receiverId, Type) ;
        System.out.println(messages);
        return ResultUtil.success(messages);
    }
    //create消息
    @PostMapping("/createMessage")
    private Result<Message> createMessage(Message message){
        //Message newMessage = messageService.createMessage(message) ;
        String senderId = message.getSenderId();
        String receiverId = message.getReceiverId();
        String messageDetail = message.getMessageDetail();
        long type = message.getType();
        String date = message.getDate();

        message.setMessageId("M"+UUID.randomUUID().toString());
        message.setDate(date);
        if (senderId.equals(receiverId)) {
            message.setType(0);
        } else {
            message.setType(1);
        }
        message.setMessageDetail(messageDetail);
        message.setSenderId(senderId);
        message.setReceiverId(receiverId);
        System.out.println("createMessage: "+message.getSenderId());
        messageService.createMessage(message);
        return ResultUtil.success(message) ;
    }

    //改变消息的type类型从0变成1 相当于垃圾桶
    @GetMapping("/changeMessages/{ReceiverId}/{SenderId}")
    public void changeMessages(@PathVariable String ReceiverId, @PathVariable String SenderId){
        messageService.changeMessages(ReceiverId, SenderId) ;
    }

    @GetMapping("/findMessagesByReceiverIdAndSenderIdAndType/{receiverId}/{senderId}/{Type}")
    public Result<Message> findMessagesByReceiverIdAndSenderId(@PathVariable String receiverId, @PathVariable String senderId, @PathVariable long Type){
        List<Message>messages = messageService.findMessagesByReceiverIdAndSenderIdAndType(receiverId, senderId,Type);
        return ResultUtil.success(messages);
    }
}
